package com.ruoyi.common.net.mqtt.multi;

import com.ruoyi.common.net.mqtt.IMessageHandler;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author xumh
 * @title: MqttClientConnect
 * @description: TODO
 * @date 2024/12/1815:03
 */
public class MqttClientConnect {
    @Autowired(required = false)
    private List<IMessageHandler> messageHandlers;
    private MqttClient mqttClient;
    private String mqttClientId;

    public MqttClientConnect() {
    }

    public void setMqttClient(String host, String clientId, String userName, String passWord, boolean cleanSession, MqttCallback mqttCallback) throws MqttException {
        MqttConnectOptions options = this.mqttConnectOptions(host, clientId, userName, passWord, cleanSession);
        if (mqttCallback == null) {
            this.mqttClient.setCallback(new MqttClientCallback(this.mqttClientId, this.messageHandlers));
        } else {
            this.mqttClient.setCallback(mqttCallback);
        }

        this.mqttClient.connect(options);
    }

    private MqttConnectOptions mqttConnectOptions(String host, String clientId, String userName, String passWord, boolean cleanSession) throws MqttException {
        this.mqttClient = new MqttClient(host, clientId, new MemoryPersistence());
        MqttConnectOptions options = new MqttConnectOptions();
        options.setUserName(userName);
        options.setPassword(passWord.toCharArray());
        options.setConnectionTimeout(30);
        options.setAutomaticReconnect(true);
        options.setCleanSession(cleanSession);
        return options;
    }

    public void close() throws MqttException {
        this.mqttClient.close();
        this.mqttClient.disconnect();
    }

    public void publish(String topic, String msg) throws MqttException {
        this.mqttClient.publish(topic, msg.getBytes(), 0, false);
    }

    public void publish(String topic, String msg, int qos) throws MqttException {
        this.mqttClient.publish(topic, msg.getBytes(), qos, false);
    }

    public void publish(String topic, byte[] payload, int qos, boolean retained) throws MqttException {
        this.mqttClient.publish(topic, payload, qos, retained);
    }

    public void subscribe(String[] topic) throws MqttException {
        this.mqttClient.subscribe(topic);
    }

    public void subscribe(String topic, int qos) throws MqttException {
        this.mqttClient.subscribe(topic, qos);
    }

    public void reconnect() throws MqttException {
        if (!this.mqttClient.isConnected()) {
            this.mqttClient.reconnect();
        }

    }

    public void unsubscribe(String topic) throws MqttException {
        this.mqttClient.unsubscribe(topic);
    }

    public String getMqttClientId() {
        return this.mqttClientId;
    }

    public void setMqttClientId(String mqttClientId) {
        this.mqttClientId = mqttClientId;
    }
}
